package com.cinema.web;

import com.cinema.entity.User;
import com.cinema.service.UserService;
import com.cinema.util.HttpUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/cinema")
public class CinemaController {

    @Autowired
    private UserService userService;
    @RequestMapping(value = "/register")
    @ResponseBody
    //注册方法
    public Map<String,Object> getRegister(HttpServletRequest request){
        //新建容器存储交互信息
        Map<String,Object> modelMap = new HashMap<>();
        User user = null;
        //处理前端传入的信息，获取user对应字符串
        String userStr = HttpUtil.getString(request,"user");
        ObjectMapper mapper = new ObjectMapper();
        try {
            //将字符串转换为user对象
            user = mapper.readValue(userStr,User.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //判断user，昵称是否为空
        if (user != null && user.getMobile() != null){
            //进行默认属性赋值
            user.setCreateTime(new Date());
            user.setLastEditTime(new Date());
            int effectedNum = userService.insertUser(user);
            if (effectedNum > 0){
                modelMap.put("success",true);
            }else {
                modelMap.put("success",false);
            }
        }else {
            modelMap.put("errMsg","请输入用户信息");
        }
        return modelMap;
    }

    @RequestMapping(value = "/login")
    @ResponseBody
    //登陆方法
    public Map<String, Object> login(HttpServletRequest request){

        Map<String,Object> modelMap = new HashMap<>();
        //接收请端发送的mobile和psw
        String mobile = HttpUtil.getString(request,"mobile");
        String psw = HttpUtil.getString(request,"psw");
        //查询是否有此用户
        User user = userService.queryUserByMobile(mobile);
        if (user == null){
            modelMap.put("errMsg","没有此用户");
        }
        else if (user.getMobile().equals(mobile) && user.getPsw().equals(psw)){
            request.getSession().setAttribute("userId",user.getUserId());
            modelMap.put("success",true);
        }else if (user.getMobile().equals(mobile)){
            modelMap.put("success",false);
            modelMap.put("errMsg","密码错误");
        }
        return modelMap;
    }
    //修改密码
    @RequestMapping(value = "updatepsw")
    @ResponseBody
    private Map<String,Object> updatePsw(HttpServletRequest request){
        Map<String,Object> modelMap = new HashMap<>();
        //获取前端传入的手机号和密码
        String mobile = HttpUtil.getString(request,"mobile");
        String psw = HttpUtil.getString(request,"psw");
        if (mobile != null && psw != null){
            int effected = userService.updatePsw(mobile,psw);
            if (effected > 0){
                modelMap.put("success",true);
            }
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }
}
